import { getEnvConfig } from './config/index'
import { request } from './utils/request'

// api静态描述
const apiMap = {
  getCaptcha: { url: '/login/captcha', method: 'get' },
  sysLogin: { url: '/login/sysLogin', method: 'post' },
  phoneNumberLogin: { url: '/wechat/phoneNumberLogin', method: 'post' }
}

// api调用封装，用于桥接环境配置、api定义和统一请求工具
async function callApi (apiKey, params, extraOptions) {
  const { apiBaseUrl } = getEnvConfig()
  const { url, method } = apiMap[apiKey]
  const formatUrl = `${apiBaseUrl}${url}`
  const options = { data: params, method, ...extraOptions }
  try {
    return request(formatUrl, options)
  } catch (e) {
    return Promise.reject(e)
  }
}

export {
  callApi
}